﻿
//# Added By WLI : 23 March 2012
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Unity;
using P2.Cuberry.Framework.Helper;
using P2.Cuberry.Modules.SimpleSCM.SCSupplierInvoice.Views.SCSupplierInvoicesFindSCSupplierView;



namespace P2.Cuberry.Modules.SimpleSCM.SCSupplierInvoice.PresentationModels
{
    public class SCSupplierInvoiceFindSCSupplierPresentationModel : INotifyPropertyChanged
    {
        public SCSupplierInvoiceFindSCSupplierPresentationModel(ISCSupplierInvoiceFindSCSupplierView view, Services.ISCSupplierInvoiceService service,
            List<P2.Cuberry.Modules.SimpleSCM.SCSupplierInvoice.BusinessEntities.CROrganisation> scSuppliers,
            IUnityContainer container, IRegionManager regionManager)
        {
            this.SupplierList   = scSuppliers;
            this.FilteredAccounts = scSuppliers;
            View = view;
            this.Service = service;

            this.SCSupplierFindCommand  = new DelegateCommand<object>(OnSCSupplierFindExecute, OnSCSupplierFindCanExecute);
            this.SCSupplierSelectCommand = new DelegateCommand<object>(OnSCSupplierSelectExecute, OnSCSupplierSelectCanExecute);
            this.SCSupplierCloseCommand  = new DelegateCommand<object>(OnSCSupplierCloseExecute, OnSCSupplierCloseCanExecute);
            this.SCSupplierRefreshCommand = new DelegateCommand<object>(OnSCSupplierRefreshExecute, OnSCSupplierRefreshCanExecute);

            View.Model = this;
        }


        #region "Members"

        string _SCSupplierNamecur = "";
        public string SCSupplierNamecur
        {
            get
            {
                return _SCSupplierNamecur;
            }
            set
            {
                if (_SCSupplierNamecur != value)
                {
                    _SCSupplierNamecur = value;
                    OnPropertyChanged("SCSupplierNamecur");
                }
            }
        }

        #endregion


        #region Commands

        public DelegateCommand<object> SCSupplierSelectCommand { get; set; }
        public DelegateCommand<object> SCSupplierCloseCommand { get; set; }
        public DelegateCommand<object> SCSupplierFindCommand { get; set; }
        public DelegateCommand<object> SCSupplierRefreshCommand { get; set; }


        void OnSCSupplierSelectExecute(object sourceAccount)
        {
            if (View.SelectedItem != null)
            {
                this.SCSupplierNamecur = View.SelectedItem.OrganisationNameCur;

                View.Close(true);
            }
        }


        bool OnSCSupplierSelectCanExecute(object sourceAccount)
        {
            return true;
        }


        void OnSCSupplierFindExecute(object sourceAccount)
        {
            try
            {
                View.UpdateFocusedControl();
                string name = SCSupplierNamecur.ToLower();
                this.FilteredAccounts = BusinessEntities.CROrganisation.GetList.Where(S => S.OrganisationNameCur.ToLower().Contains(SCSupplierNamecur.ToLower())).Select(c => c).ToList();
               // this.SupplierList = BusinessEntities.CROrganisation.GetList.Where(S => S.OrganisationNameCur.ToLower().Contains(SCSupplierNamecur.ToLower())).Select(c => c).ToList();
             
                View.Model = this;
               
            }
            catch (System.Exception ex) //AM:TC
            {
                P2ExceptionHandler.LogException(ex, string.Format("{0}.{1}", this.GetType().Name, System.Reflection.MethodInfo.GetCurrentMethod().Name));
            }
        }


        bool OnSCSupplierFindCanExecute(object sourceAccount)
        {
            return true;
        }


        void OnSCSupplierCloseExecute(object sourceAccount)
        {
            View.Close(false);
        }


        bool OnSCSupplierCloseCanExecute(object sourceAccount)
        {
            return true;
        }


        void OnSCSupplierRefreshExecute(object sourceAccount)
        {
            try
            {
                P2.Cuberry.Modules.SimpleSCM.SCSupplierInvoice.BusinessEntities.CROrganisation templateProduct = new P2.Cuberry.Modules.SimpleSCM.SCSupplierInvoice.BusinessEntities.CROrganisation();

                this.SupplierList.Clear();
                this.SupplierList = BusinessEntities.CROrganisation.GetList;
               // this.SupplierList.AddRange(Service.RetrieveSCProducts(templateProduct).Where(a => a.StatusID == 120901).ToList());

                this.FilteredAccounts = BusinessEntities.CROrganisation.GetList;
                View.Model = this;
            }
            catch (System.Exception ex) //AM:TC
            {
                P2ExceptionHandler.LogException(ex, string.Format("{0}.{1}", this.GetType().Name, System.Reflection.MethodInfo.GetCurrentMethod().Name));
            }
        }
        bool OnSCSupplierRefreshCanExecute(object sourceSCProduct)
        {
            return true;
        }

        #endregion


        public List<P2.Cuberry.Modules.SimpleSCM.SCSupplierInvoice.BusinessEntities.CROrganisation> SupplierList;
        public IEnumerable<P2.Cuberry.Modules.SimpleSCM.SCSupplierInvoice.BusinessEntities.CROrganisation> FilteredAccounts;

        public ISCSupplierInvoiceFindSCSupplierView View { get; set; }
        public Services.ISCSupplierInvoiceService Service;
        public P2.Cuberry.Modules.SimpleSCM.SCSupplierInvoice.BusinessEntities.CROrganisation Model { get; set; }


        #region Property Changed

        public event PropertyChangedEventHandler PropertyChanged;


        private void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler Handler = PropertyChanged;
            if (Handler != null) Handler(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion
    }
}
